﻿using System;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Xml;
using System.Xml.XPath;
using Com.FastSearch.SharePoint.Interfaces;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;

namespace Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI
{
    /// <summary>
    /// ESPcoreResults is the default search result list implementation which 
    /// shows the search results using xslt transormation.
    /// The default xslt is based on the MS Result list where only some properties and parts which
    /// cannot be mapped to ESP have been removed.<br></br>
    /// <br></br>
    /// To see the plain xml document the following xslt can be used:
    /// <code>
    /// &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    ///   &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
    ///   &lt;xsl:template match="/"&gt;
    ///     &lt;xmp&gt;&lt;xsl:copy-of select="*"/&gt;&lt;/xmp&gt;
    ///   &lt;/xsl:template&gt;
    /// &lt;/xsl:stylesheet&gt; 
    /// </code>   
    /// </summary>
    public class ESPCoreResults : ESPSearchConfig
    {
        #region Private variables

        /// <summary>
        /// <see cref="IXPathNavigable"/> (XmlDocument) containing the result as xml document which is used for the xslt transormation.        
        ///         
        /// </summary>
        private IXPathNavigable resultXmlDoc = null;
        #endregion
        #region WebPart Properties


        /// <summary>
        /// Turn on/off the debug flag for the result object.
        /// If using the default xslt this meanst that all fields will be shown in the results        
        /// </summary>
        [Category("Advanced Search configuration"),
         DefaultValue(false),
         Personalizable(PersonalizationScope.Shared),
         WebBrowsable(true),
         WebDisplayName("Debug Mode"),
         WebDescription("If checked the xml result object will have the debugmode flag set to true. If using the default xslt it will show all fields")]        
        public Boolean DebugResults
        {
            get; 
            set; 
        }

        #endregion

        #region ESPSearchWebPart overrides
        /// <summary>
        /// Specify the default XSLT file used for the result list.
        /// </summary>
        /// <returns>string containing the xslt</returns>
        public override string GetEmbeddedXsl()
        {
            return LoadXslFromAssembly("SearchResults.xslt");
        }                
        
        /// <summary>
        /// Return the xpath navigator for the result xml object used by the xslt transformation
        /// </summary>
        /// <param name="viewPath">View path for the xpath navigator. Not used as the root is returned</param>
        /// <returns><see cref="XPathNavigator"/>XPath Navigator used for the xslt rendering</returns>
        [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        protected override XPathNavigator GetXPathNavigator(string viewPath)
        {
            if (resultXmlDoc != null)
            {
                return resultXmlDoc.CreateNavigator();
            }
            // Empty result list
            return new XmlDocument().CreateNavigator();
        }
              
        /// <summary>
        /// Called from the UCF once the search has been performed.
        /// Store the result xml document in a private variable so it later can be used for the xslt transformation.        
        /// See <see cref="ESPSearchWebPart.SetSearchResult"/>
        /// </summary>
        /// <param name="SearchResult"><see cref="IESPSearchResult"/> object containing all result information</param>
        public override void SetSearchResult(IESPSearchResult SearchResult)
        {
            if (SearchResult != null)
            {
                SearchResult.DebugMode = DebugResults;
                resultXmlDoc = SearchResult.XmlResult;
            }            
        }
        #endregion
    }
}